<!-- 新增子卡 -->
<template>
  <el-drawer
    title="新增子卡"
    :before-close="handleClose"
    :visible.sync="addSonCardDialog"
    size="30%"
    custom-class="demo-drawer"
    ref="drawerAddSonCard"
  >
    <div class="demo-drawer__content" style="margin: 0px 5%">
      <el-form
        :model="formAddSonCard"
        :rules="rulesAddSonCard"
        ref="refAddSonCard"
        size="small"
        label-position="top"
      >
        <el-form-item
          label="归属车队"
          :label-width="formLabelWidth"
          style="margin-bottom: 30px"
          prop="fleetCardId"
        >
          <el-select
            v-model="formAddSonCard.fleetCardId"
            placeholder="请选择归属车队"
            style="width: 100%"
          >
            <el-option
              v-for="item in sonCardTableData"
              :key="item.id"
              :label="item.fleetName"
              :value="item.id"
            >
              {{ item.fleetName }}
            </el-option>
          </el-select>
        </el-form-item>
        <div class="test-form">基础信息</div>
        <el-form-item
          label="用户姓名"
          :label-width="formLabelWidth"
          style="margin-bottom: 30px"
          prop="name"
        >
          <el-input
            v-model="formAddSonCard.name"
            placeholder="请输入用户姓名"
            maxlength="25"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          :label-width="formLabelWidth"
          style="margin-bottom: 30px"
          prop="phone"
        >
          <el-input
            v-model="formAddSonCard.phone"
            placeholder="请输入手机号"
            maxlength="11"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="车牌号"
          :label-width="formLabelWidth"
          style="margin-bottom: 30px"
          prop="truckNumber"
        >
          <el-input
            v-model="formAddSonCard.truckNumber"
            placeholder="请输入车牌号"
            maxlength="12"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="demo-drawer-footer">
        <el-button @click="handleClose" size="small">取 消</el-button>
        <el-button @click="submitForm" type="primary" size="small"
          >确 定</el-button
        >
      </div>
    </div>
  </el-drawer>
</template>
<script>
import { leetCardSubAdd } from "@/api/teamCard/sonCard";
import { getFleetCardListApi } from "@/api/teamCard/teamCardList";
export default {
  name: "AddSonCard",
  props: {
    addSonCardDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      sonCardTableData: [],
      formAddSonCard: {
        fleetCardId: null,
        name: null,
        phone: null,
        truckNumber: null,
      },
      formLabelWidth: "80px",

      rulesAddSonCard: {
        fleetCardId: [
          {
            required: true,
            message: "请选择归属车队",
            trigger: ["change", "blur"],
          },
        ],
        name: [
          {
            required: true,
            message: "请输入用户姓名",
            trigger: ["change", "blur"],
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: ["change", "blur"],
          },
        ],
      },
    };
  },

  methods: {
    submitForm() {
      this.$refs.refAddSonCard.validate((valid) => {
        if (valid) {
          leetCardSubAdd(this.formAddSonCard).then((res) => {
            if (res && res.code == 200) {
              this.$message.success(res.msg);
              this.handleClose();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log("error submit!!");
        }
      });
    },

    getList() {
      getFleetCardListApi({ pageSize: 100, pageNum: 1, useType: 1 }).then(
        (res) => {
          this.sonCardTableData = res.rows;
        }
      );
    },

    handleClose() {
      this.$refs.refAddSonCard.resetFields();
      this.$emit("update:addSonCardDialog", false);
    },
  },
  watch: {
    addSonCardDialog: {
      deep: true, // 深度监听
      handler(newVal, oldVal) {
        if (newVal) {
          this.getList();
        }
      },
    },
  },
};
</script>
<style scoped lang="scss">
.test-form {
  font-size: 18px;
  margin: 50px 0px 20px 0px;
}
.demo-drawer-footer {
  width: 100%;
  position: absolute;
  right: -65%;
  bottom: 15px;
}
</style>
